package com.bosssoft.boss_exam_web.Controller;

import com.bosssoft.boss_exam_core.common.config.StorageConfig.StorageProperties;
import com.bosssoft.boss_exam_core.enums.business.QrCodeEnum;
import com.bosssoft.boss_exam_core.util.common.QrCodeUtil;
import com.bosssoft.boss_exam_core.util.web.ResponseUtil;
import com.bosssoft.boss_exam_web.common.annotation.LoginAdmin;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.net.MalformedURLException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;

@RestController
@Validated
@RequestMapping("/admin")
@Slf4j
@Api(value="二维码接口")
public class QrCodeController {

    @Autowired
    private StorageProperties properties;

    @ApiOperation(value="生成二维码", notes="根据用户所选的指定范围内的参数生成二维码" ,httpMethod="GET",response = Object.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name="参数对象", value="qrCodeParamBody", dataType = "HashMap", required=false, dataTypeClass=HashMap.class),
    })
    @RequestMapping(value = "/qrCode/manager")
    public Object getAdminQrCode(@LoginAdmin String adminId, @RequestParam Integer qrCodeType, @RequestParam String forwardUrl, @RequestParam String desc ){
        if (adminId == null) {
            return ResponseUtil.unlogin();
        }
        String storagePath = properties.getLocalProperties().getStoragePath();
        // 中心logo
        File logoFile = new File(storagePath+"/boss.jpg");
        File codeFile =null;
        String fileName = "";
        // 考试后天管理地址
        if(QrCodeEnum.EXAM_ADMIN_MANAGE_ADDR.getCode().equals(qrCodeType)){
             codeFile = new File(storagePath+"/BossExamAdmin.jpg");
            fileName= "BossExamAdmin.jpg";
        }
        // PC端考试
        else if(QrCodeEnum.EXAM_PC_ADDR.getCode().equals(qrCodeType)){
            codeFile = new File(storagePath+"/BossPcExam.jpg");
            fileName= "BossPcExam.jpg";
        }
        // Phone 考试
        else if (QrCodeEnum.EXAM_PHONE_ADDR.getCode().equals(qrCodeType)){
            codeFile = new File(storagePath+"/BossPhoneExam.jpg");
            fileName= "BossPhoneExam.jpg";
        }else {
            return null;
        }
        String filePath = QrCodeUtil.drawLogoQRCode(logoFile, codeFile, forwardUrl, desc);
        // 本地拉取图片
        Path path = Paths.get(storagePath);
        Resource file=null;
        try {
            Path bossExamAdminImgPath = path.resolve(fileName);
            file = new UrlResource(bossExamAdminImgPath.toUri());
            if (!file.exists() || !file.isReadable()) {
                return null;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        }
        if(file == null) {
            ResponseEntity.notFound();
        }
        // 图片媒体类型
        MediaType mediaType = MediaType.parseMediaType("image/jpeg");
        return ResponseEntity.ok().contentType(mediaType).body(file);
    }

    @ApiOperation(value="访问二维码", notes="根据用户所选的指定范围内的参数访问二维码" ,httpMethod="GET",response = Object.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name="参数对象", value="qrCodeParamBody", dataType = "HashMap", required=false, dataTypeClass=HashMap.class),
    })
    @GetMapping("/fetchQrcode")
    public Object fetchQrCode( @RequestParam Integer qrCodeType){
        String storagePath = properties.getLocalProperties().getStoragePath();
        String fileName = "";
        // 考试后天管理地址
        if(QrCodeEnum.EXAM_ADMIN_MANAGE_ADDR.getCode().equals(qrCodeType)){
            fileName= "BossExamAdmin.jpg";
        }
        // PC端考试
        else if(QrCodeEnum.EXAM_PC_ADDR.getCode().equals(qrCodeType)){
            fileName= "BossPcExam.jpg";
        }
        // Phone 考试
        else if (QrCodeEnum.EXAM_PHONE_ADDR.getCode().equals(qrCodeType)){
            fileName= "BossPhoneExam.jpg";
        }else {
            return null;
        }
        // 本地拉取图片
        Path path = Paths.get(storagePath);
        Resource file=null;
        try {
            Path bossExamAdminImgPath = path.resolve(fileName);
            file = new UrlResource(bossExamAdminImgPath.toUri());
            if (!file.exists() || !file.isReadable()) {
                return null;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        }
        if(file == null) {
            ResponseEntity.notFound();
        }
        // 图片媒体类型
        MediaType mediaType = MediaType.parseMediaType("image/jpeg");
        return ResponseEntity.ok().contentType(mediaType).body(file);
    }

}
